<template>
  <el-dialog
    class="theme"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model.trim="form.username" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model.trim="form.nickname" />
      </el-form-item>
      <el-form-item
        label="密码"
        prop="password"
        :rules="
          form.id
            ? []
            : [{ required: true, trigger: 'blur', message: '请输入密码' }]
        "
      >
        <el-input v-model.trim="form.password" type="password" />
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <VabImage
          :default-list="form.avatar ? [form.avatar] : []"
          :maxcount="1"
          @updateImg="updateImg"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="form.email" />
      </el-form-item>
      <!-- <el-form-item label="状态" prop="state">
        <el-radio-group v-model="form.state">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item> -->
    </el-form>

    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/userManagement'

  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        form: {
          avatar: '',
          state: 1,
        },
        roleList: [],
        rules: {
          username: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
          ],
          nickname: [
            { required: true, trigger: 'blur', message: '请输入昵称' },
          ],
          email: [{ required: true, trigger: 'blur', message: '请输入邮箱' }],
          role_id: [
            { required: true, trigger: 'change', message: '请选择角色' },
          ],
          status: [
            { required: true, trigger: 'change', message: '请选择用户状态' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(roleData, row) {
        this.roleList = roleData
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        console.log(this.form)
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            console.log(this.form)
            const { message } = await doEdit(this.form)
            this.$baseMessage(
              message,
              'success',
              false,
              'vab-hey-message-success'
            )
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
      getOriginName(name, str_rep = '&nbsp;') {
        return name.replace(str_rep, '').trim()
      },
      updateImg(val) {
        this.form.avatar = val[0] ?? ''
      },
    },
  }
</script>
